﻿using System;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;
using System.Xml.Linq;
using System.Linq;
using System.Configuration;

namespace SPProperties_Browser
{
    class Program
    {
        private static List<string> _lstSites; 

        static void Main(string[] args)
        {
            try
            {
                GetSitesforEnvironment();
                DisplayFarmProperties();
                DisplayWebAppProperties();
                DisplaySiteCollectionProperties();
                DisplaySiteProperties();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception getting props: " + ex.Message);
            }

            Console.WriteLine("Hit any key to Exit............");
            Console.ReadKey();            

        }

        private static void GetSitesforEnvironment()
        {
            Console.WriteLine("Select the environment to browse the Property Bags (E.g. 1 for INT)");

            SortedDictionary<int,string> IDtoEnvironmentMapping = new SortedDictionary<int,string>();
            IDtoEnvironmentMapping.Add(1, "INT");
            IDtoEnvironmentMapping.Add(2, "QA");
            IDtoEnvironmentMapping.Add(3, "STAGE");
            IDtoEnvironmentMapping.Add(4, "PROD");

            foreach (var mapping in IDtoEnvironmentMapping)
            {
                Console.WriteLine(String.Format("{0} : {1}", mapping.Key, mapping.Value));
            }

            int EnvironmentInput;

            bool checkValidIntInput = int.TryParse(Console.ReadLine(), out EnvironmentInput);

            if (checkValidIntInput && IDtoEnvironmentMapping.ContainsKey(EnvironmentInput))
            {
                string EnvironmentSelected = IDtoEnvironmentMapping[EnvironmentInput].ToString();

                Console.WriteLine("Environment selected: " + EnvironmentSelected);
                Console.WriteLine("Initiating Property Bags Fetch....");

                _lstSites = ConfigurationManager.AppSettings[EnvironmentSelected].Split(',').ToList();

            }

            else
            {
                Console.WriteLine("Invalid Input - Please enter a number corresponding to Menu options displayed");
                GetSitesforEnvironment();
            }

        }


        #region Property Display Methods
       
        private static void DisplayFarmProperties()
        {
            DisplayBanner("Farm Property Bag");
            using (SPSite site = new SPSite(_lstSites.First()))
            {
                SPFarm farm = site.WebApplication.Farm;

                foreach (DictionaryEntry pair in farm.Properties)
                {
                    DisplayKeyValuePair(pair);
                }
            }
        }


        private static void DisplayWebAppProperties()
        {
            foreach (string siteURL in _lstSites)
            {
                using (SPSite site = new SPSite(siteURL))
                {
                    SPWebApplication webApp = site.WebApplication;
                    DisplayBanner(String.Format("WebApp PropertyBag | {0}", site.Url));
                    foreach (DictionaryEntry pair in webApp.Properties)
                    {
                        DisplayKeyValuePair(pair);
                    }
                }
            }
        }


        private static void DisplaySiteCollectionProperties()
        {
            //Site Collection Level

            foreach (string siteURL in _lstSites)
            {
                using (SPSite site = new SPSite(siteURL))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        DisplayBanner(String.Format("SiteCollection Bag | {0}", site.Url));
                        SPPropertyBag props = web.Properties;

                        foreach (DictionaryEntry pair in props)
                        {
                            DisplayKeyValuePair(pair);                            
                        }
                    }
                }
            }       
        }

        private static void DisplaySiteProperties()
        {
            foreach (string siteURL in _lstSites)
            {
                using (SPSite site = new SPSite(siteURL))
                {
                    foreach (SPWeb web in site.AllWebs)
                    {
                        DisplayBanner(String.Format("Site Property Bag | {0}", web.Url));
                        SPPropertyBag props = web.Properties;
                        foreach (DictionaryEntry pair in props)
                        {
                            DisplayKeyValuePair(pair);
                        }                        

                    }
                }
            }
        }
        #endregion


        #region Console Display Methods

        private static void DisplayKeyValuePair(DictionaryEntry pair)
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write(pair.Key + " | ");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(pair.Value);
            Console.Write("\n");
        }

        private static void DisplayBanner(string displayString)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.DarkBlue;

            Console.WriteLine("***************************************************");
            Console.WriteLine("*                                                 *");                
            Console.WriteLine(String.Format("*      {0}*",displayString));
            Console.WriteLine("*                                                 *");
            Console.WriteLine("***************************************************");            
            Console.Write("\n\n");
            ConsoleColorReset();
        }

        private static string DisplayCharacterFrequency(string display, int frequency)
        {
            string constructedSTring = String.Empty;

            for (int i = 0; i < frequency; i++)
            {
                Console.Write(display);
                constructedSTring += display;
            }

            return constructedSTring;
        }

        private static void ConsoleColorReset()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.Black;
        }

        #endregion
    }
}

